<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=574663
-->
<head>
  <title>Test for Bug 574663</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=574663">Mozilla Bug 574663</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 574663 **/

// SimpleTest's paint_listener does not work on other windows, so we inline
// a smaller version here.
function waitForPaint(win, utils, callback) {
  win.document.documentElement.getBoundingClientRect();
  if (!utils.isMozAfterPaintPending) {
    callback();
    return;
  }

  var onpaint = function() {
    if (!utils.isMozAfterPaintPending) {
      win.removeEventListener("MozAfterPaint", onpaint);
      callback();
      return;
    }
    if (utils.isTestControllingRefreshes) {
      utils.advanceTimeAndRefresh(0);
    }
  }
  win.addEventListener("MozAfterPaint", onpaint);
  if (utils.isTestControllingRefreshes) {
    utils.advanceTimeAndRefresh(0);
  }
}

function forceScrollAndWait(scrollbox, callback) {
  let win = scrollbox.ownerDocument.defaultView;
  let utils = SpecialPowers.getDOMWindowUtils(win);

  utils.advanceTimeAndRefresh(1000);

  let postApzFlush = function() {
    SpecialPowers.Services.obs.removeObserver(postApzFlush, "apz-repaints-flushed", false);
    waitForPaint(win, utils, callback);
  }
  SpecialPowers.Services.obs.addObserver(postApzFlush, "apz-repaints-flushed", false);
  if (!utils.flushApzRepaints()) {
    postApzFlush();
  }
}

function sendTouchpadScrollMotion(scrollbox, direction, ctrl, momentum, callback) {
  var win = scrollbox.ownerDocument.defaultView;
  let event = {
    deltaMode: WheelEvent.DOM_DELTA_PIXEL,
    deltaY: direction * 3,
    lineOrPageDeltaY: direction,
    ctrlKey: ctrl,
    isMomentum: momentum
  };

  let kExtraEvents = 5;

  var received = 0;
  var onwheel = function() {
    if (++received == 1 + kExtraEvents) {
      // We have captured all the outstanding wheel events. Wait for the
      // animation to add itself to the refresh driver.
      scrollbox.removeEventListener("wheel", onwheel);
      setTimeout(function() {
        forceScrollAndWait(scrollbox, callback);
      }, 0);
    }
  };
  scrollbox.addEventListener("wheel", onwheel);

  synthesizeWheel(scrollbox, 10, 10, event, win);
  // then 5 additional pixel scrolls
  event.lineOrPageDeltaY = 0;
  for (let i = 1; i <= kExtraEvents; ++i) {
    synthesizeWheel(scrollbox, 10, 10, event, win);
  }
}

function runTest() {
  var win = open('data:text/html,<!DOCTYPE html>\n' +
    '<div id="scrollbox" style="height: 100px; overflow: auto;">' +
    '  <div style="height: 1000px;"></div>' +
    '</div>', '_blank', 'width=300,height=300');
  SimpleTest.waitForFocus(function () {
    var scrollbox = win.document.getElementById("scrollbox");
    let winUtils = SpecialPowers.getDOMWindowUtils(win);
    let outstandingTests = [
      [false, false],
      [false, true],
      [true, false],
      [true, true],
    ];

    // grab the refresh driver, since we want to make sure
    // async scrolls happen in deterministic time
    winUtils.advanceTimeAndRefresh(1000);

    function nextTest() {
      let [ctrlKey, isMomentum] = outstandingTests.shift();
      let scrollTopBefore = scrollbox.scrollTop;
      let zoomFactorBefore = winUtils.fullZoom;

      let check = function() {
        if (!ctrlKey) {
          let postfix = isMomentum ? ", even after releasing the touchpad" : "";
          // Normal scroll: scroll
          is(winUtils.fullZoom, zoomFactorBefore, "Normal scrolling shouldn't change zoom" + postfix);
          isnot(scrollbox.scrollTop, scrollTopBefore, "Normal scrolling should scroll" + postfix);
        } else {
          if (!isMomentum) {
            isnot(winUtils.fullZoom, zoomFactorBefore, "Ctrl-scrolling should zoom while the user is touching the touchpad");
            is(scrollbox.scrollTop, scrollTopBefore, "Ctrl-scrolling shouldn't scroll while the user is touching the touchpad");
          } else {
            is(winUtils.fullZoom, zoomFactorBefore, "Momentum scrolling shouldn't zoom, even when pressing Ctrl");
            isnot(scrollbox.scrollTop, scrollTopBefore, "Momentum scrolling should scroll, even when pressing Ctrl");
          }
        }

        if (!outstandingTests.length) {
          winUtils.restoreNormalRefresh();
          win.close();
          SimpleTest.finish();
          return;
        }

        // Revert the effect for the next test.
        sendTouchpadScrollMotion(scrollbox, -1, ctrlKey, isMomentum, function() {
          setTimeout(nextTest, 0);
        });
      }

      sendTouchpadScrollMotion(scrollbox, 1, ctrlKey, isMomentum, check);
    }
    nextTest();
  }, win);
}

window.onload = function() {
  SpecialPowers.pushPrefEnv({
    "set":[["general.smoothScroll", false],
           ["mousewheel.acceleration.start", -1],
           ["mousewheel.system_scroll_override_on_root_content.enabled", false],
           ["mousewheel.with_control.action", 3]]}, runTest);
}

SimpleTest.waitForExplicitFinish();

</script>
</pre>

</body>
</html>
